package com.cloudptt.api.product.func.call.group;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import com.cloudptt.api.models.call.RtcpMsg;
import com.cloudptt.api.pjsip.api.SipCallSession;
import com.cloudptt.api.pjsip.api.SipManager;
import com.cloudptt.api.product.func.IBroadcast;
import com.cloudptt.api.product.func.call.group.fsm.GroupCallConferenceListener;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.operator.CallOperator;
import com.mcptt.config.body.ConferenceInfoBody;

import org.pjsip.pjsua2.pjsip_inv_state;

import java.util.HashMap;
import java.util.Map;

public class GroupCallBroadcast extends BroadcastReceiver implements IBroadcast
{
	

	private Context ctx = null;
	private GroupCallTracker callTracker = null;
	private CallOperator operator = null;
//	private String groupId = null;
	private boolean isFull = false;
	private Map<String, Boolean> conferenceInfoMap = new HashMap<>();



	private GroupCallConferenceListener listener = null;

	public GroupCallBroadcast(Context ctx, GroupCallTracker callTracker, CallOperator operator)
	{
		this.ctx = ctx;
		this.callTracker = callTracker;
		this.operator = operator;

		this.ctx.registerReceiver(this, new IntentFilter(SipManager.ACTION_SIP_CALL_INCOMING));
		this.ctx.registerReceiver(this, new IntentFilter(SipManager.ACTION_SIP_CALL_CHANGED));
//		this.ctx.registerReceiver(this, new IntentFilter(SipManager.ACTION_SIP_VIDEO_EVENT_CHANGED));
		this.ctx.registerReceiver(this, new IntentFilter(SipManager.ACTION_SIP_MEDIA_CHANGED));
		this.ctx.registerReceiver(this, new IntentFilter(SipManager.ACTION_SIP_VIDEO_EVENT_CHANGED));
		this.ctx.registerReceiver(this, new IntentFilter(SipManager.ACTION_SIP_SPEAKING_CHANGED));
		this.ctx.registerReceiver(this, new IntentFilter(SipManager.ACTION_SIP_NOTIFY_CONFERENCE));
	}


	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		Log.debug(this.getClass(), "Action is " + action);

		if (action.equals(SipManager.ACTION_SIP_CALL_INCOMING)) 
		{
			SipCallSession callSession = intent.getParcelableExtra(SipManager.EXTRA_CALL_INFO);
			Log.debug(this.getClass(), "ACTION_SIP_CALL_INCOMING callInfo is " + callSession);
			callTracker.incomingCall(callSession);
			
		}
		else if (action.equals(SipManager.ACTION_SIP_CALL_CHANGED)) 
		{
			SipCallSession callSession = intent.getParcelableExtra(SipManager.EXTRA_CALL_INFO);
			callTracker.recvCallSession(callSession);
			int callState = callSession.getCallState();//未来多路需要带callKey做处理
			if(listener != null
				&& callState == pjsip_inv_state.PJSIP_INV_STATE_DISCONNECTED)
			{
				if(conferenceInfoMap != null) {
					conferenceInfoMap.clear();
					listener.updateConferenceInfoList("", true, conferenceInfoMap);
				}
			}
		}
		else if (action.equals(SipManager.ACTION_SIP_MEDIA_CHANGED)) {
			int mediaStatus = intent.getIntExtra(SipManager.EXTRA_MEDIA_STATUS,0);
			Log.debug(this.getClass(), "ACTION_SINGLE_CALL_INFO mediaStatus is " + mediaStatus);
			String callIdStr = intent.getStringExtra(SipManager.EXTRA_CALL_ID);
			callTracker.receiveCallMediaStatus(callIdStr,mediaStatus);
		}
		else if (action.equals(SipManager.ACTION_SIP_SPEAKING_CHANGED)) 
		{
			String callIdStr = intent.getStringExtra(SipManager.EXTRA_CALL_ID);
			String floorInfo = intent.getStringExtra(SipManager.EXTRA_SPEAKING_INFO);
			RtcpMsg rtcpMsg = new RtcpMsg(floorInfo);
			Log.debug(this.getClass(), "onReceive ACTION_SIP_SPEAKING_CHANGED callIdStr = " + callIdStr);
			Log.debug(this.getClass(), "onReceive ACTION_SIP_SPEAKING_CHANGED rtcpMsg is " + rtcpMsg);
			if(rtcpMsg != null)
			{
				callTracker.receiveRtcpMsg(callIdStr,rtcpMsg);
			}
		}
		else if (action.equals(SipManager.ACTION_SIP_VIDEO_EVENT_CHANGED)) {
			String callKey = intent.getStringExtra(SipManager.EXTRA_CALL_KEY);
			Log.debug(this.getClass(), "ACTION_SIP_VIDEO_EVENT_CHANGED callKey = " + callKey);
			callTracker.receivePCallVideoEvent(callKey);
		}
		else if( action.equals( SipManager.ACTION_SIP_NOTIFY_CONFERENCE ) )
		{

//			String infoStr = intent.getStringExtra(SipManager.Info_Str);
			String bodyStr = intent.getStringExtra(SipManager.Body_Str);
//			Log.debug(this.getClass(), "onReceive ACTION_SIP_NOTIFY_CONFERENCE infoStr = " + infoStr);
			Log.debug(this.getClass(), "onReceive ACTION_SIP_NOTIFY_CONFERENCE bodyStr = " + bodyStr);
			if( bodyStr != null
					&& bodyStr.length() > 0)
			{
				try {
//					McpttInfoBody_Conference infoBody = McpttInfoBody_Conference.parseXmlStr(infoStr);
//					Log.debug(this.getClass(), "onReceive ACTION_SIP_NOTIFY_CONFERENCE infoBody = " + infoBody.toValueString());
					ConferenceInfoBody body = ConferenceInfoBody.parseXmlStr(bodyStr);
					Log.debug(this.getClass(), "onReceive ACTION_SIP_NOTIFY_CONFERENCE body = " + body);
					if(body != null)
					{
//						String url = infoBody.getGroupId();
//						groupId = AddrInfo.toNumFromSipUrl(url);
						this.isFull = body.isFull();
						Map<String, Boolean> map = body.getConferenceInfoMap();
						Log.debug(this.getClass(), "onReceive ACTION_SIP_NOTIFY_CONFERENCE map = " + map);
						if(isFull) {
							this.conferenceInfoMap = map;
						}
						else
						{
							if(conferenceInfoMap != null
								&& map != null) {
								conferenceInfoMap.putAll(map);
							}
						}
						if(listener != null) {
							listener.updateConferenceInfoList("", isFull,conferenceInfoMap);
						}
					}
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		}
	}

	public void setListener(GroupCallConferenceListener listener) {
		Log.debug(this.getClass(), "GroupCallBroadcast clearResource");
		this.listener = listener;
		if(listener != null)
		{
			listener.updateConferenceInfoList("",isFull,conferenceInfoMap);
		}
	}

	public void clearConferenceInfoMap()
	{
		Log.debug(this.getClass(), "GroupCallBroadcast clearConferenceInfoMap");
		if(conferenceInfoMap != null) {
			this.conferenceInfoMap.clear();
		}
	}

	public void clearResource()
	{
		Log.debug(this.getClass(), "GroupCallBroadcast clearResource");
		this.ctx.unregisterReceiver( this );
		this.ctx = null;
		this.callTracker = null;
		this.listener = null;
//		this.curCallSession = null;
	}
}
